var roleId=-1;
var divId="";
var flag=true;
var assignUserRoles = {
    init : function() {
    	roleId = getUrlParam("roleId");
    	divId="userrole"+roleId;
        // 初始化事件绑定
    	assignUserRoles.evenBind();
    	assignUserRoles.loadUserRole();

    },
    evenBind : function() {
        // ----------------------------------------------------------------------->
        // 设置用户菜单表单操作
        // ----------------------------------------------------------------------->
        // 取消
        $('#cancelAssignUserRoles').bind('click', function() {
        	assignUserRoles.closeDiv();
        });

        // 提交
        $('#submitAssignUserRoles').bind('click', function() {
        	if(flag){
	    		flag=false;
	    		var $roleId = $('#assignRoleId').val();
	             // var $data=$('#assignUserRolesGrid').treegrid('getData');
	             var checkedObjs = $("input[type=checkbox][name=id]:checked");

	             var ids = [];
	             var i = 0;
	             if (checkedObjs.size() > 0) {
	                 checkedObjs.each(function() {
	                     ids[i++] = $(this).val();
	                 });
	             }else{
	            	 $.messager.alert('提示', '请勾选需要分配的用户!', 'warning');
	                 return false;
	             }
	             ajaxProcess({
	                 url : getProjectUrl("/bfs/dept/deptuser/add"),
	                 requestType : "POST",
	                 traditional : true,
	                 data : {
	                     ids : ids,
	                     roleId : roleId
	                 },
	                 successHandle : function() {
	                	 flag=true;
	                	 assignUserRoles.closeDiv();
	                 }
	             });
        	}
        	 
         });
    },  
    closeDiv : function() {   	
        do{
           window.parent.$('#'+divId).dialog('destroy');
           var win=window.parent.$('#'+divId);
        }while(win);
    },
    refreshData : function() {
    	assignUserRoles.loadUserRole();
    },
    beforeLoad : function() {
        $('#createUserDiv').dialog('destroy', true);
        $('#editUserDiv').dialog('destroy', true);
        $('#setUserMenuDiv').dialog('destroy', true);
        $('#setUserResourceDiv').dialog('destroy', true);
    },
    // ----------------------------------------------------------------------->
    // 打开设置用户菜单的窗口
    // ----------------------------------------------------------------------->
    loadUserRole : function() {
    	$('#assignUserRolesGrid').treegrid({
            url : getProjectUrl('/bfs/dept/deptuser/treelist'),
            queryParams : {
                roleId : roleId
            },
            idField : 'id',
            treeField : 'text',
            columns : [ [ {
                field : 'id',
                title : 'ID',
                width : 100,
                checkbox : true
            }, {
                title : '部门人员',
                field : 'text',
                width : 180,
                align:'left'
            } ] ],
            singleSelect : false,
            rownumbers : true,
            method : 'POST',
            fitColumns : true,
            animate: true,
            checkbox : false,
            checkOnSelect : false,
            onClickRow : function(row) {
                var $row = $("input[type=checkbox][name=id][value=" + row.id + "]")[0];
                $row.checked = !$row.checked;
                nodeClick($('#assignUserRolesGrid'), $row);
            },
           onLoadSuccess : function(row, data) {
            	uncheckAll();
                $("input[type=checkbox][name=id]").click(function() {
                	nodeClick($('#assignUserRolesGrid'), this);
                });
                checkedMenu(data.rows);
            }
        });
        $('#assignRoleId').val(roleId);      
    },
};
$(function() {
    resetSize();
    assignUserRoles.init();
    resetSize();
});

